# Create your models here.

from django.db import models

from hosts_mgr.models import HostList


class AlertContactPerson(models.Model):
    """告警联系人表"""
    name = models.CharField(max_length=45, verbose_name="联系人名", unique=True)
    phone_number = models.CharField(null=True, max_length=11, verbose_name="手机号码")
    email = models.EmailField(null=True, verbose_name="邮箱")

    feishu_bot = models.CharField(null=True, max_length=128, verbose_name="飞书群机器人")
    feishu_bot_sign = models.CharField(null=True, max_length=128, verbose_name="飞书群机器人安全签名")

    ding_talk = models.CharField(null=True, max_length=128, verbose_name="钉钉")
    ding_talk_sign = models.CharField(null=True, max_length=128, verbose_name="钉钉安全签名")

    qw_id = models.CharField(null=True, max_length=128, verbose_name="企微_企业ID")
    qw_robot_secret = models.CharField(null=True, max_length=128, verbose_name="企微_自建应用Secret")
    qw_agent_id = models.CharField(null=True, max_length=128, verbose_name="企微_自建应用AgentId")
    qw_to_user = models.CharField(null=True, max_length=128, verbose_name="企微_用户账号ID")

    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "告警联系人表"
        db_table = "monitor_alert_contact_person"


class AlertContactGroup(models.Model):
    """告警联系组表"""
    name = models.CharField(max_length=45, verbose_name="联系组名", unique=True)
    remark = models.TextField(null=True, verbose_name="描述")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "告警联系组表"
        db_table = "monitor_alert_contact_group"


class AlertContactPersonGroup(models.Model):
    """告警联系人和告警联系组关联表"""
    person = models.ForeignKey(AlertContactPerson, on_delete=models.CASCADE, verbose_name="关联告警联系人")
    group = models.ForeignKey(AlertContactGroup, on_delete=models.CASCADE, verbose_name="关联告警联系组")

    class Meta:
        verbose_name = "告警联系人和告警联系组关联表"
        db_table = "monitor_alert_contact_person_group"


class MonitorGroup(models.Model):
    """监控分组表"""
    name = models.CharField(max_length=45, verbose_name="分组名称", unique=True)
    sort_num = models.SmallIntegerField(verbose_name="显示顺序")
    create_time = models.DateTimeField(null=True, verbose_name="创建时间", auto_now_add=True)

    class Meta:
        verbose_name = "监控分组表"
        db_table = "monitor_alert_monitor_group"


class MonitorTask(models.Model):
    """监控任务表"""
    enable_status = models.SmallIntegerField(null=True, verbose_name="任务启用状态(1：启用，2：禁用)")
    task_status = models.SmallIntegerField(default=0, verbose_name="任务执行状态(0:未验证，1：成功，2：失败)")
    mon_group = models.ForeignKey(MonitorGroup, on_delete=models.PROTECT, verbose_name="关联监控分组表",
                                  related_name='t_mon_group')
    num_of_fail = models.SmallIntegerField(default=0, verbose_name="任务执行失败次数")
    type_choices = (
        (1, '站点检测'),
        (2, '端口检测'),
        (3, '进程检测'),
        (4, '自定义脚本'),
    )
    type = models.SmallIntegerField(choices=type_choices,
                                    verbose_name="监控类型(1：站点检测，2：端口检测，3：进程检测，4：自定义脚本)")
    name = models.CharField(max_length=45, verbose_name="监控名称", unique=True)
    mon_path = models.CharField(max_length=90, verbose_name="监控地址")
    host = models.ForeignKey(HostList, null=True, on_delete=models.PROTECT, verbose_name="关联主机列表")
    timeout = models.IntegerField(null=True, verbose_name="超时时间(ms)")
    mon_freq = models.CharField(max_length=24, verbose_name="监控频率")
    trigger_threshold = models.CharField(max_length=24, verbose_name="触发阈值")
    alert_method_choices = (
        ("1", "邮箱"),
        ("2", "飞书"),
        ("3", "企业微信"),
        ("4", "钉钉"),
    )
    alert_method_list = models.JSONField(verbose_name="告警方式(1：邮箱，2：飞书，3：企业微信，4：钉钉)")
    last_alert_time = models.IntegerField(default=0, verbose_name="上一次告警的时间戳")
    quiet_time = models.CharField(max_length=24, verbose_name="沉默时间(分钟数)")
    remark = models.TextField(null=True, verbose_name="备注")
    create_time = models.DateTimeField(null=True, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(null=True, auto_now=True, verbose_name="更新时间")

    fail_time = models.DateTimeField(null=True, auto_now=True, verbose_name="故障时间")
    recovery_time = models.DateTimeField(null=True, auto_now=True, verbose_name="恢复时间")
    sort_num = models.SmallIntegerField(verbose_name="显示顺序")

    class Meta:
        verbose_name = "监控任务表"
        db_table = "monitor_alert_monitor_task"


class MonitorTaskAlertGroup(models.Model):
    """监控任务与告警联系组的关联表"""
    monitor_task = models.ForeignKey(MonitorTask, on_delete=models.CASCADE, verbose_name="关联监控任务")
    alert_group = models.ForeignKey(AlertContactGroup, on_delete=models.CASCADE, verbose_name="关联告警联系组")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = "监控任务与告警联系组关联表"
        db_table = "monitor_task_alert_group"


class AlertLogs(models.Model):
    """告警记录表"""
    name = models.ForeignKey(MonitorTask, on_delete=models.CASCADE, verbose_name="监控名称")
    mon_group = models.CharField(max_length=45, verbose_name="监控组名称")
    type = models.CharField(max_length=24, verbose_name="监控类型")
    mon_path = models.CharField(max_length=90, verbose_name="监控地址")
    alert_status = models.SmallIntegerField(null=True, verbose_name="告警状态(0：告警中，1：已恢复)")
    recv_alert_group = models.CharField(max_length=128, verbose_name="接收告警组")
    duration_time = models.SmallIntegerField(verbose_name="持续时间(分钟数)")
    alert_send_time = models.DateTimeField(null=True, db_index=True, auto_now_add=True, verbose_name="发送告警时间")
    first_alert_time = models.DateTimeField(null=True, verbose_name="第一次告警时间")

    class Meta:
        verbose_name = "告警记录表"
        db_table = "monitor_alert_center_alert_logs"
